package com.superman.supermarket.client.view.store;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.MobileUtils;
import com.superman.mvpframe.client.widget.lazy.LazyImage;
import com.superman.mvpframe.client.widget.pulltorefresh.IPullToRefreshView;
import com.superman.mvpframe.client.widget.pulltorefresh.PullToRefreshViewAbstractImpl;
import com.superman.mvpframe.client.widget.toggle.ToggleItem;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent.ToggleSelectEventHandler;
import com.superman.supermarket.client.composite.common.banner.ImageBanner;
import com.superman.supermarket.client.composite.home.recommandgoods.RecommandGoodsModule;
import com.superman.supermarket.client.composite.store.CateCellModule;
import com.superman.supermarket.client.composite.store.CateListModule;
import com.superman.supermarket.client.event.PageJumpEvent;

/**
 * 店铺
 * 
 * @author superman
 * @version 2018年8月2日下午5:41:42
 */
public class StoreViewImpl extends PullToRefreshViewAbstractImpl implements StoreView {

	interface StoreViewImplUiBinder extends UiBinder<Widget, StoreViewImpl> {
	}

	private static StoreViewImplUiBinder uiBinder = GWT.create(StoreViewImplUiBinder.class);

	@UiField
	CateListModule catelistmodule;
	@UiField
	FlowPanel couponpanel, wrapperpanel, skulistpanel;
	@UiField
	Button detailbtn;
	@UiField
	ImageBanner imagebanner;
	private StoreViewPresenter presenter;
	@UiField
	LazyImage storelogo;

	@UiField
	HTML storenamehtml, deschtml, couponnamehtml;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月14日 下午11:20:50
	 */
	public StoreViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		imagebanner.setPixelSize(MobileUtils.px2(750), MobileUtils.px2(370));

		couponpanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				presenter.pushToStoreCouponListPage();
			}
		}, ClickEvent.getType());
	}

	@Override
	public IPullToRefreshView getPullToRefreshView() {
		return this;
	}

	@Override
	public int getWrapperPanelAbsTop() {
		return skulistpanel.getAbsoluteTop();
	}

	@Override
	public void init() {
		catelistmodule.addToggleSelectEventHandler(new ToggleSelectEventHandler() {

			@Override
			public void onSelected(ToggleSelectEvent event) {
				ToggleItem toggleItem = event.getToggleItem();
				if (toggleItem instanceof CateCellModule) {
					CateCellModule cm = (CateCellModule) toggleItem;
					Map<String, Object> map = cm.getData();
					String cid = String.valueOf(map.get("CATALOG_ID"));
					presenter.refreshSkuListByCateId(cid);
				}
			}
		});
	}

	@Override
	public void removeCateModule() {
		catelistmodule.removeFromParent();
	}

	@Override
	public void removeCouponModule() {
		couponpanel.removeFromParent();
	}

	@Override
	public void selectFirstCate() {
		catelistmodule.selectByIndex(0);
	}

	@Override
	public void setCateList(List<Map<String, Object>> list) {
		catelistmodule.setListData(list);
	}

	@Override
	public void setCouponName(String couponName) {
		couponnamehtml.setText("店铺优惠券:" + couponName);
	}

	@Override
	public void setDataListDivId(String id) {
		skulistpanel.getElement().setId(id);
	}

	@Override
	public void setListData(List<Map<String, Object>> list) {
		for (Map<String, Object> map : list) {
			RecommandGoodsModule goodsModule = new RecommandGoodsModule();
			goodsModule.setData(map);
			skulistpanel.add(goodsModule);
		}

	}

	@Override
	public void setMeScrollDivId(String id) {
		wrapperpanel.getElement().setId(id);
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (StoreViewPresenter) presenter;
	}

	@Override
	public void setSkuImages(List<Map<String, Object>> list) {
		imagebanner.clear();
		for (int i = 0; i < list.size(); i++) {
			Image image = new Image(String.valueOf(list.get(i).get("IMAGE_ADDR")));
			imagebanner.addItem(image);
		}
		imagebanner.init(false);
	}

	@Override
	public void setStoreBaseInfo(final Map<String, Object> map) {
		storenamehtml.setText(String.valueOf(map.get("NAME")));
		if (map.get("LOGO") != null)
			storelogo.setUrl(String.valueOf(map.get("LOGO")));
		if (map.get("DESCRIPTION") != null)
			deschtml.setText(String.valueOf(map.get("DESCRIPTION")));
		if (map.get("DETAIL_ADDR") != null) {
			detailbtn.setVisible(true);
			detailbtn.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					XGinjector.INSTANCE.getEventBus().fireEvent(new PageJumpEvent(String.valueOf(map.get("DETAIL_ADDR"))));
				}
			});
		}

	}

}
